package com.atguigu.tingshu.account.receiver;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @author atguigu-mqx
 * @ClassName AccountReceiver
 * @description: TODO
 * @date 2023年11月27日
 * @version: 1.0
 */
@Component
public class AccountReceiver {

    @Autowired
    private UserAccountService userAccountService;

    /**
     * 监听初始化用户账户信息
     *
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_USER_REGISTER)
    public void userRegister(ConsumerRecord<String, String> record) {
        //  获取到消费数据
        System.out.println(record.key());
        String userId = record.value();
        //  判断
        if (!StringUtils.isEmpty(userId)) {
            //  调用服务层方法.
            userAccountService.initUserAccount(Long.parseLong(userId));
        }
    }

    /**
     * 监听扣减余额
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_MINUS)
    public void accountMinus(ConsumerRecord<String, String> record) {
        //  获取消息数据
        String orderNo = record.value();
        //  判断
        if (!StringUtils.isEmpty(orderNo)){
            //  扣减余额
            userAccountService.minus(orderNo);
        }

    }

    /**
     * 监听解锁金额
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_UNLOCK)
    public void unlock(ConsumerRecord<String, String> record) {
        //  获取消息数据
        String orderNo = record.value();
        //  判断
        if (!StringUtils.isEmpty(orderNo)){
            //  扣减余额
            userAccountService.unlock(orderNo);
        }

    }

    /**
     * 监听充值成功记录.
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS)
    public void rechargePaySuccess(ConsumerRecord<String, String> record) {
        //  获取消息数据
        String orderNo = record.value();
        //  判断
        if (!StringUtils.isEmpty(orderNo)){
            //  充值金额
            userAccountService.addAccount(orderNo);
        }
    }
}